/* bsp_config.c */
#include "bsp_config.h"

void BSP_InitStdioUART(uint32_t baudrate)
{
    /* Configure the GPIO ports */
    GPIO_InitPara  GPIO_InitStructure;
    USART_InitPara  USART_InitStructure;

    /* Enable GPIOA clock */
    RCC_APB2PeriphClock_Enable( RCC_APB2PERIPH_GPIOB, ENABLE );
    /* Enable USART1 APB clock */
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_USART1, ENABLE );
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_AF, ENABLE);
    GPIO_InitStructure.GPIO_Pin     = GPIO_PIN_6 ;
    GPIO_InitStructure.GPIO_Mode    = GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Speed   = GPIO_SPEED_50MHZ;
    GPIO_Init( GPIOB , &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin     = GPIO_PIN_7;
    GPIO_InitStructure.GPIO_Mode    = GPIO_MODE_IN_FLOATING;;
    GPIO_Init( GPIOB , &GPIO_InitStructure);

    GPIO_PinRemapConfig(GPIO_REMAP_USART1, ENABLE);
    
    //USART_DeInit(USART1 );
    USART_InitStructure.USART_BRR                 = baudrate;
    USART_InitStructure.USART_WL                  = USART_WL_8B;
    USART_InitStructure.USART_STBits              = USART_STBITS_1;
    USART_InitStructure.USART_Parity              = USART_PARITY_RESET;
    USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
    USART_InitStructure.USART_RxorTx              = USART_RXORTX_RX | USART_RXORTX_TX;
    USART_Init(USART1, &USART_InitStructure);

    /* USART enable */
    USART_Enable(USART1, ENABLE);	    
}

int fputc(int c, FILE *f)
{
    while (USART_GetBitState(USART1, USART_FLAG_TBE) == RESET)
    {
    }
    USART_DataSend(USART1, c);

    return c;
}

int fgetc(FILE *f)
{   
    while (USART_GetBitState(USART1, USART_FLAG_RBNE) == RESET)
    {
    }
    return USART_DataReceive(USART1);
}

/* EOF. */
